﻿/**********************************************
 * 功能名称：设置重分配的路径
编写人：  杨莹
建立时间： 杨 2011年04月24日
说明：

 * 选择作业中心，显示当前作业中心对应的成本账户ID。
 * 成本账户ID可以进行增删改。
 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using System.Text;
using System.Collections.Generic;

namespace Hisun.View.Calculation
{
    public partial class PIPTransfer : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alter;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MyDataBind();
            }
        }

        private void MyDataBind()
        {
            string CurrentAccountPeriod = Application["accountPeriod"].ToString();
            var MyQuery = from a in db.TABC_JS_ProductInProcessTransfor
                          join b in db.TABC_JS_ProductInProcess on a.中间体ID equals b.中间体ID
                          join c in db.TABC_JS_ProductInProcess on a.目标中间体ID equals c.中间体ID
                          where a.会计期间ID == CurrentAccountPeriod
                          select new
                          {
                              a.ID,
                              转出中间体ID = a.中间体ID,
                              转出中间体名称 = b.中间体名称,
                              目标中间体ID = a.目标中间体ID,
                              目标中间体名称 = c.中间体名称

                          };

            GridView_DataQuery.DataSource = MyQuery;
            GridView_DataQuery.DataBind();
            
        
        }

        protected void Button_Add_Click(object sender, EventArgs e)
        {
            Panel_Main.Visible = false;
            Panel_Add.Visible = true;
            FullDropdownList();
        }

        private void FullDropdownList()
        {
            var CenterList = from a in db.TABC_JS_CostCenter
                               where new string[] { "A", "BF", "BG", "BZ","BH" }.Contains(a.类型)
                               && a.停用 == false
                               select new 
                               {
                                   a.作业中心ID,
                                   a.作业中心名称
                               };
            if (CenterList.Count() != 0)
          {
              DropDownList_CenterList_Start.Items.Clear();
              DropDownList_CenterList_Final.Items.Clear();
             foreach(var CenterRow in CenterList)
             {
                DropDownList_CenterList_Start.Items.Add(new ListItem("("+CenterRow.作业中心ID.ToString()+")"+CenterRow.作业中心名称,CenterRow.作业中心ID.ToString()));
             
                 DropDownList_CenterList_Final.Items.Add(new ListItem("("+CenterRow.作业中心ID.ToString()+")"+CenterRow.作业中心名称,CenterRow.作业中心ID.ToString()));
             }
             
          
          }
          
        }

        //点击保存当前作业中心。
        protected void Button_Save_Click(object sender, EventArgs e)
        {

            string StartInfo = DropDownList_CenterList_Start.SelectedValue + DropDownList_PIP_Start.SelectedValue;
            string FinalInfo = DropDownList_CenterList_Final.SelectedValue + DropDownList_PIP_Final.SelectedValue;
            if (StartInfo == FinalInfo)
            {
                alter = "<script>alert('目标作业中心与中间体不正确');</script>";
                return;
            }
            else
            {

                var ValidQuery = from a in db.TABC_JS_ProductInProcessTransfor
                                 where a.中间体ID == DropDownList_PIP_Start.SelectedValue
                                 && a.目标中间体ID == DropDownList_PIP_Final.SelectedValue
                                 && a.会计期间ID == Application["accountPeriod"].ToString()
                                 select a;
                if (ValidQuery.Count() > 0)
                {

                    alter = "<script>alert('当前转出路径已存在，请重新选择');</script>";
                    return;
                
                }



                string PIP_Final = DropDownList_PIP_Final.SelectedValue;
                var MyAddQuery = (from a in db.TABC_JS_ProductInProcess
                                where a.中间体ID == PIP_Final
                                select new 
                                {
                                 a.中间体ID,
                                 a.作业中心ID,
                                 a.产品ID
                                }).Single();

                TABC_JS_ProductInProcessTransfor MyAddRow = new TABC_JS_ProductInProcessTransfor();
                MyAddRow.操作员ID = Session[ServerVars.sysSessionUserName].ToString();
                MyAddRow.会计期间ID = Application["accountPeriod"].ToString();
                MyAddRow.中间体ID = DropDownList_PIP_Start.SelectedValue;
                MyAddRow.目标中间体ID = MyAddQuery.中间体ID;
                MyAddRow.目标作业中心ID = MyAddQuery.作业中心ID;
                MyAddRow.目标产品ID = MyAddQuery.产品ID;

                db.TABC_JS_ProductInProcessTransfor.InsertOnSubmit(MyAddRow);
                try
                {
                    db.SubmitChanges();
                    alter = "<script>alert('存当前转出路径成功');</script>";
                    Panel_Main.Visible = true;
                    Panel_Add.Visible = false;
                    MyDataBind();

                }
                catch 
                {
                    alter = "<script>alert('保存当前转出路径失败');</script>";
                }
                
              
            }


        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {
            Panel_Main.Visible = true;
            Panel_Add.Visible = false;
            MyDataBind();
        }

        protected void Button_Del_Command(object sender, CommandEventArgs e)
        {
            string DelId = e.CommandArgument.ToString();
            var DelQuery = from a in db.TABC_JS_ProductInProcessTransfor
                           where a.ID.ToString() == DelId
                           select a;
            if (DelQuery.Count() == 1)
            {
                db.TABC_JS_ProductInProcessTransfor.DeleteAllOnSubmit(DelQuery);

                try
                {
                    db.SubmitChanges();
                    alter = "<script>alert('删除当前转出路径成功');</script>";
                    MyDataBind();

                }
                catch
                {
                    alter = "<script>alert('删除当前转出路径失败');</script>";
                }
                
              
            
            
            }
        }

 
    }
}
